﻿using System;
using System.Timers;
using System.Threading;
using System.Data.SqlClient;

using Builderator.Core.Action;
using Builderator.Core.Plugin;
using Builderator.Core.Interfaces;

namespace Builderator.Core.Trigger
{
    public delegate void TriggerDelegate(Guid buildIdentifier);

    public class TriggerBase : ActionBase, ITrigger
    {
        public event TriggerDelegate TriggerEvent;

        public TriggerBase(string name, Guid identifier)
            : base(name, identifier)
        {
        
        }

        protected void TriggerBuild()
        {
            if (this.TriggerEvent != null)
                this.TriggerEvent(this.Associated);
        }

        public virtual void Start()
        {

        }

        public virtual void Stop()
        {

        }

        public override string Select(DatabaseTable table)
        {
            switch (table)
            {
                case DatabaseTable.Actions:
                    return base.Select(table);
                case DatabaseTable.Plugins:
                    return string.Format("SELECT * FROM plugins WHERE identifier='{0}'", this.Type.ToString());
                default:
                    return string.Empty;
            }
        }

        public override string Insert(DatabaseTable table)
        {
            switch (table)
            {
                case DatabaseTable.Actions:
                    return base.Insert(table);
                case DatabaseTable.Plugins:
                    return string.Format("INSERT INTO plugins VALUES '{0}', '{1}')", this.Type, this.GetType().FullName);
                default:
                    return string.Empty;
            }
        }

        public override string Delete(DatabaseTable table)
        {
            switch (table)
            {
                case DatabaseTable.Actions:
                    return base.Delete(table);
                case DatabaseTable.Plugins:
                    return string.Format("DELETE FROM plugins WHERE identifier='{0}'", this.Type.ToString());
                default:
                    return string.Empty;
            }
        }
    }
}